package com.lb.excel;

import cn.hutool.poi.excel.ExcelWriter;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhangjun
 * @Date: 2024-06-27
 * @Description:
 */
public class HutoolTest {
    public static void main(String[] args) throws IOException, IllegalAccessException {
        FileInputStream fis = new FileInputStream("C:\\Users\\PC\\推送进度报表_20240627.xlsx");
        Workbook workbook = new XSSFWorkbook(fis);
        fis.close();

        // 获取工作表
        Sheet sheet = workbook.getSheetAt(0);

        // 替换表头中的变量
        String exporter = "0158";
        String stringCellValue = sheet.getRow(1).getCell(0).getStringCellValue();
        stringCellValue = stringCellValue.replace("${exporter}", exporter);
        sheet.getRow(1).getCell(0).setCellValue(stringCellValue);

        ExcelWriter writer = new ExcelWriter(workbook, "sheet1");

        // 从第2行开始写入数据，保留第1行作为表头
        int startRow = 4;
        List<PushProgressDto> rows = new ArrayList<>();
        PushProgressDto dto = new PushProgressDto();
        dto.setDepartmentId(1L);
        dto.setDepartmentName("天猫");
        dto.setVerifyCount(10L);
        rows.add(dto);

        for (int i = 0; i < rows.size(); i++) {
            PushProgressDto dto1 = rows.get(i);
            Class<?> clazz = dto1.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                if (field.getName().equals("departmentId")) {
                    continue;
                }
                field.setAccessible(true);
                Object o = field.get(dto1);
                System.out.println(field.getName() + "," + o);
            }
//            writer.writeCellValue(0, startRow + i, dto1.getDepartmentName());
        }

        // 保存到新文件
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PC\\推送进度数据_20240430_1.xls");
        writer.getWorkbook().write(fos);
        fos.close();

        writer.close();

        for (int i = 1; i <= 14; i++) {
            int output = (i + 1) % 2;
            System.out.println("循环到 " + i + " 时输出 " + output);
        }
    }
}
